package com.aiit.controller;

import com.aiit.pojo.bo.User;
import com.aiit.service.CheckLoginService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("login")
public class CheckLoginController {
    @Autowired
    private CheckLoginService checkLoginService;


    /**
     * 登录检验
     * @param id
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Login",method = RequestMethod.POST,produces ={"application/json;charset=UTF-8"})
    public String checkLogin(@RequestParam long id, @RequestParam String password, HttpServletRequest request){
        User user=checkLoginService.checkLogin(id, password);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("count", 1);
        map.put("data", user);
        if(user!=null){
            map.put("code", 0);
            map.put("msg", "登录成功");
            HttpSession session=request.getSession();
            session.setAttribute("user",user);
        }else{
            map.put("code",-1);
            map.put("msg","用户名或密码错误");
        }
        return JSONObject.toJSONString(map);
    }

}
